﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using JasRibbonApplicatie.Classes;
using JasRibbonApplicatie.Model;
using System.Linq;

namespace JasRibbonApplicatie
{

    public partial class MateriaalOfToebehorenPicker : UserControl
    {
        private List<Materiaaltype> materiaalTypes;
        private List<ToebehorenType> toebehorenTypes;
        public Materiaaltype selectedMateriaalType { get; private set; }
        public ToebehorenType selectedToebehorenType {get; private set;}
        private string[] materiaalSoorten;
        private static MateriaalModel mm = new MateriaalModel();

        public static readonly RoutedEvent selectionChangeEvent = EventManager.RegisterRoutedEvent(
        "selectionChange", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(MateriaalOfToebehorenPicker));

        // Provide CLR accessors for the event
        public event RoutedEventHandler selectionChange
        {
            add { AddHandler(selectionChangeEvent, value); }
            remove { RemoveHandler(selectionChangeEvent, value); }
        }

        public MateriaalOfToebehorenPicker()
        {
            this.InitializeComponent();
            materiaalTypes = mm.GetAllMateriaalType();
            toebehorenTypes = mm.GetAllToebehorenTypes();
        }
        private void ToebehorenOfMateriaal_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var oldselectedMateriaalType = selectedMateriaalType;
            var oldselectedToebehorenType = selectedToebehorenType;
            selectedMateriaalType = null;
            selectedToebehorenType = null;

            TypeListbox.Visibility = Visibility.Collapsed;
            if (ToebehorenOfMateriaal.SelectedItem != null)
            {
                if ((ToebehorenOfMateriaal.SelectedItem as ListBoxItem).Content as string == "Toebehoren")
                {
                    SoortListBox.ItemsSource = toebehorenTypes;
                    SoortListBox.DisplayMemberPath = "naam";
                }
                else
                {
                    materiaalSoorten = (from types in materiaalTypes
                                        select types.type).Distinct().ToArray();
                    SoortListBox.ItemsSource = materiaalSoorten;
                    SoortListBox.DisplayMemberPath = "";
                }
            }

            if(selectedToebehorenType != oldselectedToebehorenType || selectedMateriaalType != oldselectedMateriaalType)
                RaiseEvent(new RoutedEventArgs(MateriaalOfToebehorenPicker.selectionChangeEvent));
        }
        private void SoortListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var oldselectedMateriaalType = selectedMateriaalType;
            var oldselectedToebehorenType = selectedToebehorenType;
            selectedMateriaalType = null;
            selectedToebehorenType = null;

            TypeListbox.Visibility = Visibility.Collapsed;
            if (SoortListBox.SelectedItem != null)
            {
                if (SoortListBox.SelectedItem is ToebehorenType)
                {
                    selectedToebehorenType = SoortListBox.SelectedItem as ToebehorenType;
                    
                }
                else
                {
                    TypeListbox.ItemsSource = materiaalTypes.Where(m => m.type == SoortListBox.SelectedItem.ToString()).ToList();
                    TypeListbox.Visibility = Visibility.Visible;
                }
            }
            if (selectedToebehorenType != oldselectedToebehorenType || selectedMateriaalType != oldselectedMateriaalType)
                RaiseEvent(new RoutedEventArgs(MateriaalOfToebehorenPicker.selectionChangeEvent));
        }
        private void TypeListbox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var oldselectedMateriaalType = selectedMateriaalType;
            var oldselectedToebehorenType = selectedToebehorenType;
            selectedMateriaalType = null;
            selectedToebehorenType = null;

            if (TypeListbox.SelectedItem != null)
            {
                selectedMateriaalType = TypeListbox.SelectedItem as Materiaaltype;
            }
            else
            {
                selectedMateriaalType = null;
            }
            if (selectedToebehorenType != oldselectedToebehorenType || selectedMateriaalType != oldselectedMateriaalType)
                RaiseEvent(new RoutedEventArgs(MateriaalOfToebehorenPicker.selectionChangeEvent));
        }

        
    }
}